package com.zhitong.ztblog.controllers;

import com.zhitong.ztblog.services.servicesImp.FileService;
import com.zhitong.ztblog.utils.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.Map;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class FileContro {

    @Autowired
    FileService fileService;

    @PostMapping("/image")
    public AjaxResponse uploadImage(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
        Map<String, String> m= fileService.uploadImage(multipartFile, request);
        return AjaxResponse.success(m);
    }

    @DeleteMapping("/image")
    public AjaxResponse deleteImage(@PathParam("realPath") String realPath){
        boolean b = fileService.deleteImage(realPath);
        if (!b){
            AjaxResponse error=new AjaxResponse();
            error.setCode(500);
            error.setMessage("图片删除失败");
            return error;
        }
        AjaxResponse success = AjaxResponse.success();
        success.setMessage("图片删除成功");
        return success;
    }

}
